﻿using System;
using System.Text;

namespace IssueIT.Core.Configuration.Settings
{
	public interface IConversionIssueReporter
	{
		void ReportIssues(ISettings settings);
	}

	public class ConversionIssueReporter : IConversionIssueReporter
	{
		public void ReportIssues(ISettings settings)
		{
			if( !settings.HasIssues )
			{
				return;
			}

			int counter = 0;
			StringBuilder builder = new StringBuilder();
			builder.Append("Could not convert all input values into their expected types:");
			builder.Append(Environment.NewLine);
			foreach(ConversionIssue issue in settings.Issues)
			{
				builder.AppendFormat("---- Problem[{0}] --------------------", counter++);
				builder.Append(Environment.NewLine);
				builder.AppendFormat("\tItem type:       {0}", (issue.Item!=null) ? issue.Item.GetType().FullName : "(null)");
				builder.Append(Environment.NewLine);
				builder.AppendFormat("\tProperty:        {0}", issue.Property.Name);
				builder.Append(Environment.NewLine);
				builder.AppendFormat("\tProperty Type:   {0}", issue.Property.PropertyType.FullName);
				builder.Append(Environment.NewLine);
				builder.AppendFormat("\tAttempted Value: {0}", issue.Value);
				builder.Append(Environment.NewLine);
				builder.AppendFormat("\tMessage:         {0}", issue.Message);
				builder.Append(Environment.NewLine);
				builder.AppendFormat("\tException:       {0}", issue.Exception);
				builder.Append(Environment.NewLine);
			}
			throw new InvalidOperationException(builder.ToString());
		}
	}
}